package com.yycx.base.provider.handler;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.module.export.ExportField;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.ExportInterceptor;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.module.admin.client.entity.BaseUserAccountLogs;
import com.yycx.module.admin.provider.service.BaseUserAccountLogsService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 登录日志导出扩展
 *
 * @author zyf
 */
@Component("baseUserAccountLogsPageListHandler")
public class BaseUserAccountLogsPageListHandler implements PageInterceptor<BaseUserAccountLogs>, ExportInterceptor {

    @Resource
    private BaseUserAccountLogsService baseUserAccountLogsService;

    @Override
    public void prepare(CriteriaQuery<BaseUserAccountLogs> cq, PageParams pageParams, EntityMap params) {
        String loginBeginDate = cq.getParams("loginBeginDate");
        String loginEndDate = cq.getParams("loginEndDate");
        String account = cq.getParams("account");
        String loginIp = cq.getParams("loginIp");
        if(FlymeUtils.isNotEmpty(loginBeginDate)){
            cq.ge("loginTime",loginBeginDate);
        }
        if(FlymeUtils.isNotEmpty(loginEndDate)){
            cq.le("loginTime",loginEndDate);
        }
        if(FlymeUtils.isNotEmpty(account)){
            cq.eq("account",account);
        }
        if(FlymeUtils.isNotEmpty(loginIp)){
            cq.eq("loginIp",loginIp);
        }
        cq.select(BaseUserAccountLogs.class, "*");
        cq.orderByDesc("loginTime");
    }


    @Override
    public void initExcelExportEntity(ExportField exportField, List list) {
        Object key = exportField.getKey();
        exportField.setWidth(20);
    }

    @Override
    public void complete(CriteriaQuery<BaseUserAccountLogs> cq, List<EntityMap> result, EntityMap extra) {
        Long userId = OpenHelper.getUserId();
        if (FlymeUtils.isNotEmpty(userId)) {
            cq.setExportKey("loginLog:" + userId);
        }
    }
}
